﻿using DAO.DAOUtils;
using DTO.BaseDTO;
using DTO.DTOUtils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace DAO.BaseDAO
{
    public class RealAddressDAO
    {
        public RealAddressDTO GetByAddressId(String addressId)
        {
            List<Parameter> listParam = new List<Parameter>();
            listParam.Add(new Parameter(DTOConstant.AddressId, addressId));

            DataTable result = SQLUtils.ExcuteQuery("RealAddressGetByAddressId", ref listParam);
            return CreateRealAddressObject(ref result);
        }

        #region Insert Command
        public bool InsertAddressOnly(ref RealAddressDTO address)
        {
            bool res = true;

            List<Parameter> listParam = new List<Parameter>();
            listParam.Add(new Parameter(DTOConstant.AddressNumber, address.AddressNumber));
            listParam.Add(new Parameter(DTOConstant.WardId, address.WardId));

            address.AddressId = "";
            Parameter output = new Parameter(DTOConstant.AddressId, address.AddressId);
            res = SQLUtils.ExcuteNoneQuery("AddressInsert",
                ref listParam,
                ref output);
            address.AddressId = output.Value.ToString();

            return res;
        }

        public bool InsertProvine(ref ProvinceDTO province)
        {
            bool res = true;

            List<Parameter> listParam = new List<Parameter>();
            listParam.Add(new Parameter(DTOConstant.ProvinceName, province.ProvinceName));

            province.Id = "";
            Parameter output = new Parameter(DTOConstant.ProvinceId, province.Id);
            res = SQLUtils.ExcuteNoneQuery("ProvinceInsert",
                ref listParam,
                ref output);
            province.Id = output.Value.ToString();

            return res;
        }

        public bool InsertDistrict(ref DistrictDTO district)
        {
            bool res = true;

            List<Parameter> listParam = new List<Parameter>();
            listParam.Add(new Parameter(DTOConstant.ProvinceId, district.ProvinceId));
            listParam.Add(new Parameter(DTOConstant.DistrictName, district.DistrictName));

            district.Id = "";
            Parameter output = new Parameter(DTOConstant.DistrictId, district.Id);
            res = SQLUtils.ExcuteNoneQuery("DistrictInsert",
                ref listParam,
                ref output
                );
            district.Id = output.Value.ToString();

            return res;
        }

        public bool InsertWard(ref WardDTO ward)
        {
            bool res = true;

            List<Parameter> listParam = new List<Parameter>();
            listParam.Add(new Parameter(DTOConstant.WardName, ward.WardName));
            listParam.Add(new Parameter(DTOConstant.DistrictId, ward.DistrictId));

            Parameter output = new Parameter(DTOConstant.WardId, ward.Id);
            ward.Id = "";
            res = SQLUtils.ExcuteNoneQuery("WardInsert",
                ref listParam,
                ref output
                );
            ward.Id = output.Value.ToString();

            return res;
        } 
        #endregion

        #region Get Command

        public DataTable GetAllProvince()
        {
            List<Parameter> listParam = new List<Parameter>();
            return SQLUtils.ExcuteQuery("ProvinceGetAll", ref listParam);
        }

        public DataTable GetAllDistrictByProvince(String provinceId)
        {
            List<Parameter> listParam = new List<Parameter>();
            listParam.Add(new Parameter(DTOConstant.ProvinceId, provinceId));

            return SQLUtils.ExcuteQuery("DistrictByProvinceId", ref listParam);
        }

        public DataTable GetAllWardByDistrict(String districtId)
        {
            List<Parameter> listParam = new List<Parameter>();
            listParam.Add(new Parameter(DTOConstant.DistrictId, districtId));

            return SQLUtils.ExcuteQuery("WardByDistrictId", ref listParam);
        } 
        #endregion

        public RealAddressDTO CreateRealAddressObject(ref DataTable result)
        {
            DataRow row = result.Rows[0];
            return new RealAddressDTO(
                    row[DTOConstant.AddressId].ToString(),
                    row[DTOConstant.AddressNumber].ToString(),
                    row[DTOConstant.WardId].ToString(),
                    row[DTOConstant.WardName].ToString(),
                    row[DTOConstant.DistrictId].ToString(),
                    row[DTOConstant.DistrictName].ToString(),
                    row[DTOConstant.ProvinceId].ToString(),
                    row[DTOConstant.ProvinceName].ToString()
                );
        }
    }
}
